/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.builders;

import javax.swing.JPanel;
import org.balisunrise.annotation.util.AnnotationsUtilitiesException;
import org.balisunrise.annotation.util.NameUtil;
import org.balisunrise.annotation.util.SearchUtilities;
import org.balisunrise.application.Application;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.EntityActions;
import org.balisunrise.common.EntityGrid;
import org.balisunrise.common.EntitySelection;
import org.balisunrise.common.VisualAction;
import org.balisunrise.swing.components.panel.SelectionPanel;
import org.balisunrise.swing.components.panel.layouts.SearchLayoutBuilder;

/**
 *
 * @author glauco
 */
public class EntitySelectionBuilder implements
        org.balisunrise.builder.EntitySelectionBuilder{

    private static final String MESSAGE = "Erro ao construir uma EntitySelection.\n";

    @Override
    public EntitySelection create(Class entityClass, String alias)
            throws BuilderException {

        if(entityClass == null){
            throw new BuilderException(MESSAGE +
                    "entityClass não pode ser nulo.");
        }

        if(alias == null){
            throw new BuilderException(MESSAGE +
                    "alias não pode ser nulo.");
        }

        try{
            SearchUtilities su = SearchUtilities.create(entityClass, alias);
            return create(su);
        }catch(AnnotationsUtilitiesException aue){
            throw new BuilderException(MESSAGE +
                    aue.getMessage(), aue.getCause());
        }
    }

    @Override
    public EntitySelection create(SearchUtilities su) throws BuilderException {

        if(su == null){
            throw new BuilderException(MESSAGE +
                    "SearchUtilities não pode ser nulo.");
        }

        org.balisunrise.swing.commons.EntitySelection es =
                new org.balisunrise.swing.commons.EntitySelection();

        es.setAlias(su.alias);
        es.setEntityClass(su.entityClass);
        es.setTitle(su.title);

        // cria o EntityGrid
        EntityGrid<? extends JPanel> eg = Application.get()
                .getVisualEntityManager()
                .getEntityGridBuilder()
                .create(su.gridUtilities);

        // Cria o Painel de Ações
        EntityActions<VisualAction, ? extends JPanel> ea = Application.get()
                .getVisualEntityManager()
                .getEntityActionBuilder()
                .create(su.entityClass,
                        su.alias);

        // atribui o pai
        eg.setOwner(es);
        ea.setOwner(es);

        // atribui as entidades visuais
        es.setEntityGrid(eg);
        es.setEntityActions(ea);

        // atribui representacao visual
        es.setVisualRepresentation(new SelectionPanel(
                eg.getVisualRepresentation(),
                ea.getVisualRepresentation(),
                new SearchLayoutBuilder()));

        // nome do painel
        String name = NameUtil.buildName(es);
        es.getVisualRepresentation().setName(name);
        es.getVisualRepresentation().getAccessibleContext().setAccessibleName(name);
        es.setName(name);

        return es;
    }
}
